package com.edu.exam.entity.grading.audit;

import com.edu.exam.enums.grading.ReportType;
import com.edu.exam.enums.grading.ReportFormat;
import com.edu.exam.enums.grading.ReportStatus;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 成绩审核报告实体类
 *
 * <p>所属模块：B-06 成绩管理模块</p>
 * <p>对应数据库表：grade_audit_report</p>
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
public class GradeAuditReport {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 报告ID
     */
    @Column(name = "report_id", nullable = false, length = 50, unique = true)
    private String reportId;

    /**
     * 报告名称
     */
    @Column(name = "report_name", nullable = false, length = 200)
    private String reportName;

    /**
     * 报告类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "report_type", nullable = false, length = 20)
    private ReportType reportType = ReportType.SUMMARY;

    /**
     * 报告描述
     */
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 报告模板
     */
    @Column(name = "report_template", length = 100)
    private String reportTemplate;

    /**
     * 报告格式
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "report_format", length = 10)
    private ReportFormat reportFormat = ReportFormat.PDF;

    /**
     * 生成者ID
     */
    @Column(name = "generator_id", nullable = false, length = 50)
    private String generatorId;

    /**
     * 生成者姓名
     */
    @Column(name = "generator_name", nullable = false, length = 100)
    private String generatorName;

    /**
     * 生成时间
     */
    @Column(name = "generate_time", nullable = false)
    private LocalDateTime generateTime;

    /**
     * 报告状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private ReportStatus status = ReportStatus.PENDING;

    /**
     * 报告周期开始时间
     */
    @Column(name = "report_period_start")
    private LocalDate reportPeriodStart;

    /**
     * 报告周期结束时间
     */
    @Column(name = "report_period_end")
    private LocalDate reportPeriodEnd;

    /**
     * 任务批次ID
     */
    @Column(name = "task_batch_id", length = 50)
    private String taskBatchId;

    /**
     * 工作流ID
     */
    @Column(name = "workflow_id", length = 50)
    private String workflowId;

    /**
     * 报告URL
     */
    @Column(name = "report_url", length = 500)
    private String reportUrl;

    /**
     * 报告文件路径
     */
    @Column(name = "file_path", length = 500)
    private String filePath;

    /**
     * 文件大小（字节）
     */
    @Column(name = "file_size")
    private Long fileSize;

    /**
     * 文件校验码
     */
    @Column(name = "file_checksum", length = 32)
    private String fileChecksum;

    /**
     * 生成参数
     */
    @Column(name = "generation_parameters", columnDefinition = "TEXT")
    private String generationParameters;

    /**
     * 报告数据
     */
    @Column(name = "report_data", columnDefinition = "TEXT")
    private String reportData;

    /**
     * 报告摘要
     */
    @Column(name = "report_summary", columnDefinition = "TEXT")
    private String reportSummary;

    /**
     * 过滤条件
     */
    @Column(name = "filter_conditions", columnDefinition = "TEXT")
    private String filterConditions;

    /**
     * 排序规则
     */
    @Column(name = "sort_rules", columnDefinition = "TEXT")
    private String sortRules;

    /**
     * 生成耗时（毫秒）
     */
    @Column(name = "generation_duration")
    private Long generationDuration;

    /**
     * 记录数量
     */
    @Column(name = "record_count")
    private Integer recordCount;

    /**
     * 是否公开
     */
    @Column(name = "is_public", nullable = false)
    private Boolean isPublic = false;

    /**
     * 是否自动生成
     */
    @Column(name = "is_auto_generated", nullable = false)
    private Boolean isAutoGenerated = false;

    /**
     * 是否已发送
     */
    @Column(name = "is_sent", nullable = false)
    private Boolean isSent = false;

    /**
     * 发送时间
     */
    @Column(name = "send_time")
    private LocalDateTime sendTime;

    /**
     * 接收者列表
     */
    @Column(name = "recipients", columnDefinition = "TEXT")
    private String recipients;

    /**
     * 访问次数
     */
    @Column(name = "access_count", nullable = false)
    private Integer accessCount = 0;

    /**
     * 最后访问时间
     */
    @Column(name = "last_access_time")
    private LocalDateTime lastAccessTime;

    /**
     * 过期时间
     */
    @Column(name = "expire_time")
    private LocalDateTime expireTime;

    /**
     * 报告标签
     */
    @Column(name = "report_tags", columnDefinition = "TEXT")
    private String reportTags;

    /**
     * 报告备注
     */
    @Column(name = "report_notes", columnDefinition = "TEXT")
    private String reportNotes;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 创建用户ID
     */
    @Column(name = "create_user_id", length = 50)
    private String createUserId;

    /**
     * 更新用户ID
     */
    @Column(name = "update_user_id", length = 50)
    private String updateUserId;

    /**
     * 是否删除
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    /**
     * 删除用户ID
     */
    @Column(name = "delete_user_id", length = 50)
    private String deleteUserId;

}